package com.liuxin.zhiyuan.shujuzhongxin.service.impl;

import com.liuxin.zhiyuan.shujuzhongxin.domain.dto.XueYuanListDTO;
import com.liuxin.zhiyuan.shujuzhongxin.mapper.VolunteerRecommendationTempMapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxin.common.utils.PageUtils;
import com.liuxin.common.utils.Query;

import com.liuxin.zhiyuan.shujuzhongxin.domain.po.VolunteerRecommendationTempEntity;
import com.liuxin.zhiyuan.shujuzhongxin.service.VolunteerRecommendationTempService;


@Service
public class VolunteerRecommendationTempServiceImpl extends ServiceImpl<VolunteerRecommendationTempMapper, VolunteerRecommendationTempEntity> implements VolunteerRecommendationTempService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<VolunteerRecommendationTempEntity> page = this.page(
                new Query<VolunteerRecommendationTempEntity>().getPage(params),
                new QueryWrapper<VolunteerRecommendationTempEntity>()
        );
        return new PageUtils(page);
    }


    @Override
    public void saveOrUpdateByVrId(VolunteerRecommendationTempEntity volunteerRecommendation) {
        // 根据vrID查询现有记录
        QueryWrapper<VolunteerRecommendationTempEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vr_id", volunteerRecommendation.getVrId());
        queryWrapper.orderByDesc("cishu");
        queryWrapper.last("limit 1");
        VolunteerRecommendationTempEntity existingRecord = this.getOne(queryWrapper);
        if (existingRecord != null) {
            // 如果记录存在，更新次数和其他字段
            VolunteerRecommendationTempEntity volunteerRecommendationTempEntity = new VolunteerRecommendationTempEntity();
            volunteerRecommendationTempEntity.setVrId(existingRecord.getVrId());
            volunteerRecommendationTempEntity.setCishu(existingRecord.getCishu() + 1);
            volunteerRecommendationTempEntity.setXueyuanId(existingRecord.getXueyuanId());
            volunteerRecommendationTempEntity.setTiaojian(volunteerRecommendation.getTiaojian());
            volunteerRecommendationTempEntity.setJieguo(volunteerRecommendation.getJieguo());
            this.save(volunteerRecommendationTempEntity);
        } else {
            // 如果记录不存在，创建新记录，次数设为1
            volunteerRecommendation.setCishu(1);
            volunteerRecommendation.setVrId(volunteerRecommendation.getVrId());
            this.save(volunteerRecommendation);
        }
    }

    @Override
    public PageUtils queryPageByVrId(Map<String, Object> params) {
        // 构建查询条件
        String vrId = (String) params.get("vrId");
        IPage<VolunteerRecommendationTempEntity> page = new Query<VolunteerRecommendationTempEntity>().getPage(params);
        IPage<XueYuanListDTO> resultPage = this.baseMapper.queryPageByVrId(page, vrId);
        return new PageUtils(resultPage);
    }

}